package com.itheima.reader;

import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;

public class prac03 {

    public static void main(String[] args) throws IOException {
      //实现数字排序
        FileReader fr = new FileReader("c.txt");
        StringBuilder sb = new StringBuilder();

        int ch;
        while((ch=fr.read())!=-1)
        {
            sb.append((char)ch);
        }
        fr.close();
        System.out.println(sb);

        //使用文件流快速实现,,从容器这一步开始，成为流，转换为字符串进行切割，实现类型转换，存入整型数组
        Object[] arr = Arrays.stream(sb.toString().split("-")).map(Integer::parseInt).sorted().toArray(Integer[]::new);

        FileWriter fw = new FileWriter("d.txt");
        String s = Arrays.toString(arr).replace(",", "-");
        String resu = s.substring(1, s.length() - 1);
        fw.write(resu);
        fw.close();


    }

    private static void method1() throws IOException {
        FileReader fr = new FileReader("c.txt");
        StringBuilder sb = new StringBuilder();
        int ch;
        while ((ch=fr.read())!=-1)
        {
            sb.append((char)ch);//读取数据并加入到容器中才能做操作
        }
        fr.close();
        // System.out.println(sb);//2-1-9-4-7-8

        //对容器内的数据
        String str = sb.toString();
        String[] arr = str.split("-");
        //  System.out.println(Arrays.toString(arr));//[2, 1, 9, 4, 7, 8]

        //排序
        ArrayList<Integer> list = new ArrayList<>();
        for (String s : arr) {
            list.add(Integer.parseInt(s));
        }
        Collections.sort(list);
        System.out.println(list);//[1, 2, 4, 7, 8, 9]

        //写入
        FileWriter fw = new FileWriter("e.txt");
        for (int i = 0; i <list.size() ; i++) {
            if(i==list.size()-1)
            {
                fw.write(list.get(i)+"");
            }
            else
            {
                fw.write(list.get(i)+"-");
            }
        }
        fw.close();
    }
}
